var conf = require('./config').media;

var NodeMediaServer = require('node-media-server');

var config = {
    rtmp: {
        port: conf.rtmp,
        chunk_size: 600000,
        gop_cache: true,
        ping: 60,
        ping_timeout: 30
    },
    http: {
        port: conf.http,
        allow_origin: '*'
    }
};

var nms = new NodeMediaServer(config);
nms.run();

nms.on('preConnect', (id, args) => {
    console.log('[NodeEvent on preConnect]', `id=${id} args=${JSON.stringify(args)}`);
    // let session = nms.getSession(id);
    // session.reject();
});

nms.on('postConnect', (id, args) => {
    console.log('[NodeEvent on postConnect]', `id=${id} args=${JSON.stringify(args)}`);
});

nms.on('doneConnect', (id, args) => {
    console.log('[NodeEvent on doneConnect]', `id=${id} args=${JSON.stringify(args)}`);
});

nms.on('prePublish', (id, StreamPath, args) => {
    console.log('[NodeEvent on prePublish]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`);
    // let session = nms.getSession(id);
    // session.reject();
});

nms.on('postPublish', (id, StreamPath, args) => {
    console.log('[NodeEvent on postPublish]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`);
});

nms.on('donePublish', (id, StreamPath, args) => {
    console.log('[NodeEvent on donePublish]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`);
});

nms.on('prePlay', (id, StreamPath, args) => {
    console.log('[NodeEvent on prePlay]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`);
    // let session = nms.getSession(id);
    // session.reject();
});

nms.on('postPlay', (id, StreamPath, args) => {
    console.log('[NodeEvent on postPlay]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`);
});

nms.on('donePlay', (id, StreamPath, args) => {
    console.log('[NodeEvent on donePlay]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`);
});

var net = require('net');
var server = net.createServer();
var slaves = null;
var client = null;
server.on('connection', function (socket) {
    console.log('Tcp connection from ' + socket.remoteAddress + ' CONNECTED.');
    socket.setKeepAlive(true);
    socket._timestamp = new Date().getTime();
    socket.on('error', function (e) {
        //NET require event 'error' must be registered.
    });
    socket.on('close', function () {
        console.log('Tcp connection from ' + socket.remoteAddress + ' CLOSED.');
        //socket.destroy();
    });
    socket.on('timeout', function(){

    });
    socket.on('data', function (data) {
        var str = data.toString();
        if(str === 'Slave') {
            console.log('Slave connected.');
            slaves = socket;
            return;
        }
        else if (str === 'Client') {
            console.log('Client connected.');
            client = socket;
            return;
        }
        try {
            var obj = JSON.parse(str);
            if(obj.command && slaves) {
                slaves.write(data);
            }
            else if(client) {
                client.write(data);
            }
        }catch(e){}
    });
});
server.listen(conf.tcpServerPort, '0.0.0.0', function () {

});
server.on('error', function (e) {
    console.log(e)
});
